<template>
    <div class="ygmsg">  
    <el-form :model="form" status-icon  label-width="100px" class="demo-ruleForm">
        <div  class="form-row">
            <el-form-item label="菜品名称：" >
     <el-input v-model="form.name" placeholder="填写菜品名称"></el-input>
   </el-form-item>
   <el-form-item label="菜品分类：" >
    <el-select v-model="form.categoryName" placeholder="请选择菜品分类">
    <el-option
      v-for="item in data"
      :key="item.id"
      :label="item.name"
      :value="item.id">
    </el-option>
  </el-select>
   </el-form-item>
</div>
   <el-form-item label="菜品价格：">
     <el-input v-model="form.price" placeholder="设置菜品价格"></el-input>
   </el-form-item>

   <el-form-item label="口味做法配置：" >
     <el-input v-model="form.phone" placeholder="添加口味"></el-input>
   </el-form-item>
   <el-form-item label="菜品图片：">
    <el-upload
  class="avatar-uploader"
  action="http://localhost:9528/dev-api/common/upload"
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload">
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
   </el-form-item>
    <el-form-item label="活动形式">
    <el-input type="textarea" v-model="form.desc" style="width: 600px; height: 200px" ></el-input>
  </el-form-item>
      <div class="divider"></div>
   <div class="button-container">
    <el-button  @click="quxiao">取消</el-button>
    <el-button type="primary" @click="baocun">保存</el-button>
    <el-button style="background-color: #000000; color: #ffffff; border-color: #000000;" @click="baocuntianjia">保存并继续添加</el-button>
   </div>
  
  </el-form>
    </div>
  
</template>
<script>
import { createDish,getList } from '@/api/caipin' 
  export default {
    created() {
      getList({type:1}).then((res)=>{
          this.data=res.data
          console.log('Received items:', this.data);
        })
},
    data() {
      return {
        form: {
          categoryName: '',
          name: '',
          price: '',
          image: ''
        },
        data:[],
        imageUrl: ''
      }
    },
    methods: {
        handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      quxiao() {
        console.log(this.items)
        this.form = {};
        this.$router.push({name:"caipin"})
      },
      baocun(){
        createDish(this.form).then(()=>{
          this.form = {};
          this.$router.push({name:"caipin"})
        })     
      },
      baocuntianjia(){
        createDish(this.form).then(()=>{
          this.form = {};
        })     
      }
    }
  }
</script>
<style lang="scss" scoped>
.ygmsg{
    padding-top: 50px;
    padding-left: 120px ;
    .form-row{
        display: flex;
    }
}
.el-form-item{
    width: 400px;
}
.divider {
  height: 1px;
  background-color: #e4e7ed; /* 浅灰色分隔线 */
  margin: 20px 0; /* 上下间距 */
  width: 1200px; /* 与表单项同宽 */
}
.button-container{
    display: flex;
    gap: 12px; /* 按钮间距 */
    // justify-content: center; 
    padding-left: 400px; 
    padding-top: 15px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>